package com.example.ddcoursequery;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * Created by admin on 2015/12/15.
 */
public class DBData {
    private MyDbHelper myDbHelper;
    public DBData(MyDbHelper myDbHelper){
        this.myDbHelper = myDbHelper;
    }

    public boolean getAllTeachers(){
        boolean flag = false;
        SQLiteDatabase sqLiteDatabase = myDbHelper.getReadableDatabase();
        String count_sql = "select count(*) as count from teacher;";
        Cursor cursor = sqLiteDatabase.rawQuery(count_sql,null);
        if (cursor.moveToFirst()){
            int total = cursor.getInt(cursor.getColumnIndex("count"));
            Log.i("lyj","database teacher total = "+total);
            if(total != 0){
                String getall_sql = "select teacher_id,teacher_name,teacher_dep,teacher_sex,teacher_title,teacher_cached from teacher";
                Cursor teachercursor = sqLiteDatabase.rawQuery(getall_sql,null);
                if (teachercursor.moveToFirst()) {
                    do {
                        AppTeacher appTeacher = new AppTeacher();
                        String teacher_id = teachercursor.getString(teachercursor.getColumnIndex("teacher_id"));
                        appTeacher.setTeacher_id(teacher_id);
                        String teacher_name = teachercursor.getString(teachercursor.getColumnIndex("teacher_name"));
                        appTeacher.setTeacher_name(teacher_name);
                        String teacher_dep = teachercursor.getString(teachercursor.getColumnIndex("teacher_dep"));
                        appTeacher.setTeacher_dep(teacher_dep);
                        String teacher_sex = teachercursor.getString(teachercursor.getColumnIndex("teacher_sex"));
                        appTeacher.setTeacher_sex(teacher_sex);
                        String teacher_title = teachercursor.getString(teachercursor.getColumnIndex("teacher_title"));
                        appTeacher.setTeacher_title(teacher_title);
                        String cache = teachercursor.getString(teachercursor.getColumnIndex("teacher_cached"));
                        if ("true".equals(cache)){
                            appTeacher.setTeacher_isCached(true);
                        }else{
                            appTeacher.setTeacher_isCached(false);
                        }
                        CacheTeacher.teacherList.add(appTeacher);
                    } while (teachercursor.moveToNext());
                    flag = true;
                    Log.i("lyj", "DBData CacheTeacher.teacherList.size()=" + CacheTeacher.teacherList.size());
                }
                teachercursor.close();
            }
        }
        cursor.close();
        return flag;
    }
}
